Some reference notes on using the dotnet cli and .net core.
Project templates can be created using the dotnet cli. As of v2.4.1 (sdk) the following are the template types. Note this can be seen with the ‘new’ command.
lee@macbook:projects$ dotnet new
Usage: new [options]
Options:
-h, --help Displays help for this command.
-l, --list Lists templates containing the specified name. If no name is specified, lists all templates.
-n, --name The name for the output being created. If no name is specified, the name of the current directory is used.
-o, --output Location to place the generated output.
-i, --install Installs a source or a template pack.
-u, --uninstall Uninstalls a source or a template pack.
--nuget-source Specifies a NuGet source to use during install.
--type Filters templates based on available types. Predefined values are "project", "item" or "other".
--force Forces content to be generated even if it would change existing files.
-lang, --language Filters templates based on language and specifies the language of the template to create.
Templates Short Name Language Tags
----------------------------------------------------------------------------------------------------------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
Unit Test Project mstest [C#], F#, VB Test/MSTest
NUnit 3 Test Project nunit [C#], F#, VB Test/NUnit
NUnit 3 Test Item nunit-test [C#], F#, VB Test/NUnit
xUnit Test Project xunit [C#], F#, VB Test/xUnit
Razor Page page [C#] Web/ASP.NET
MVC ViewImports viewimports [C#] Web/ASP.NET
MVC ViewStart viewstart [C#] Web/ASP.NET
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App razor [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
Razor Class Library razorclasslib [C#] Web/Razor/Library/Razor Class Library
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
global.json file globaljson Config
NuGet Config nugetconfig Config
Web Config webconfig Config
Solution File sln Solution
Examples:
dotnet new mvc --auth Individual
dotnet new xunit
dotnet new --help
The following shows an example of created a new webapi project, building it up and running it.
lee@macbook:projects$ cd mysample3
total 0
drwxr-xr-x 2 lee 59904 64 Oct 25 10:45 .
drwxr-xr-x 11 lee 59904 352 Oct 25 10:45 ..
lee@macbook:projects/mysample3$ dotnet new webapi
The template "ASP.NET Core Web API" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on /Users/lee/projects/mysample3/mysample3.csproj...
Restoring packages for /Users/lee/projects/mysample3/mysample3.csproj...
Generating MSBuild file /Users/lee/projects/mysample3/obj/mysample3.csproj.nuget.g.props.
Generating MSBuild file /Users/lee/projects/mysample3/obj/mysample3.csproj.nuget.g.targets.
Restore completed in 805.25 ms for /Users/lee/projects/mysample3/mysample3.csproj.
Restore succeeded.
lee@macbook:projects/mysample3$ dotnet restore
Restore completed in 45.76 ms for /Users/lee/projects/mysample3/mysample3.csproj.
lee@macbook:projects/mysample3$ dotnet build
Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 48.62 ms for /Users/lee/projects/mysample3/mysample3.csproj.
mysample3 -> /Users/lee/projects/mysample3/bin/Debug/netcoreapp2.1/mysample3.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.95
lee@macbook:projects/mysample3$ dotnet run
Using launch settings from /Users/lee/projects/mysample3/Properties/launchSettings.json...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using '/Users/lee/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Development
Content root path: /Users/lee/projects/mysample3
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 15.0623ms 307
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET https://localhost:5001/
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 123.9421ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET https://localhost:5001/api/values
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Route matched with {action = "Get", controller = "Values"}. Executing action mysample3.Controllers.ValuesController.Get (mysample3)
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action method mysample3.Controllers.ValuesController.Get (mysample3) - Validation state: Valid
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action method mysample3.Controllers.ValuesController.Get (mysample3), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 0.6411ms.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'System.String[]'.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action mysample3.Controllers.ValuesController.Get (mysample3) in 70.9156ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 115.6336ms 200 application/json; charset=utf-8
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET https://localhost:5001/favicon.ico
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 0.1432ms 404
The dotnet cli also supports a watch feature where it detects file changes and automatically recompiles the source. The following shows the watch running while I made some code changes to the source:
lee@macbook:Projects/webapi$ dotnet watch run
watch : Started
Using launch settings from /Users/lee/projects/webapi/Properties/launchSettings.json...
: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using '/Users/lee/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Development
Content root path: /Users/lee/projects/webapi
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Application is shutting down...
watch : Exited
watch : File changed: /Users/lee/projects/webapi/Controllers/ValuesController.cs
watch : Started
Using launch settings from /Users/lee/projects/webapi/Properties/launchSettings.json...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using '/Users/lee/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Development
Content root path: /Users/lee/projects/webapi
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET https://localhost:5001/api/values
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Route matched with {action = "Get", controller = "Values"}. Executing action mysample3.Controllers.ValuesController.Get (mysample3)
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action method mysample3.Controllers.ValuesController.Get (mysample3) - Validation state: Valid
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action method mysample3.Controllers.ValuesController.Get (mysample3), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 0.3181ms.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'System.String[]'.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action mysample3.Controllers.ValuesController.Get (mysample3) in 71.2634ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 217.7651ms 200 application/json; charset=utf-8
Next I’d like to add a NuGet package to the webapi project. I can do this running the ‘add package’ command.
lee@macbook:/webapi$ dotnet add package Newtonsoft.Json Writing /var/folders/yh/fh50zyj15bdgbv5291f1cyvw001th0/T/tmp82hW0X.tmp info : Adding PackageReference for package 'Newtonsoft.Json' into project '/Users/lee/webapi/webapi.csproj'. log : Restoring packages for /Users/lee/webapi/webapi.csproj... info : CACHE https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project '/Users/lee/webapi/webapi.csproj'. info : PackageReference for package 'Newtonsoft.Json' version '11.0.2' added to file '/Users/lee/webapi/webapi.csproj'.
References